package pl.edu.agh.io.sylvia.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


import pl.edu.agh.io.sylvia.entity.DBObject;


public abstract class GenericDAO {
	
	protected static Connection connection = null;
	private static final String host = "localhost";
	private static final String dbName = "sylvia";
	private static final String user = "root";
	private static final String password = "";

	protected Logger log = Logger.getLogger(getClass().getName());
	
	public GenericDAO() {
		if (connection == null) {
			try {
				Class.forName("com.mysql.jdbc.Driver").getInterfaces();
				connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbName, user, password);
			} catch (ClassNotFoundException e) {
				log.log(Level.SEVERE, e.getMessage(), e);
			} catch (SQLException e) {
				log.log(Level.SEVERE, e.getMessage(), e);
			}
			if (connection != null) {
				log.log(Level.INFO, "Connection successfully created.");
			} else {
				log.log(Level.SEVERE, "Cannot connect to DB.");
			}
		}
	}
	
	public abstract DBObject getById(int id);
	
	public abstract List<? extends DBObject> getAll();
}
